<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>${post.title} - 智界论坛</title>
    <link rel="stylesheet" href="css/tech-style.css">
</head>
<body>
<nav class="navbar">
    <div class="main-container">
        <a href="index.jsp" class="navbar-brand">智界论坛</a>
        <ul class="navbar-nav">
            <li><a href="index.jsp">首页</a></li>
            <li><a href="postForm.jsp">发帖</a></li>
            <c:choose>
                <c:when test="${not empty sessionScope.user}">
                    <li><a href="logout">注销</a></li>
                </c:when>
                <c:otherwise>
                    <li><a href="login.jsp">登录</a></li>
                    <li><a href="register.jsp">注册</a></li>
                </c:otherwise>
            </c:choose>
        </ul>
    </div>
</nav>

<main class="main-container" style="padding-top: 2rem; padding-bottom: 2rem;">
    <jsp:useBean id="questionDAO" class="com.example.dao.QuestionDAO" scope="page"/>
    <jsp:useBean id="answerDAO" class="com.example.dao.AnswerDAO" scope="page"/>

    <c:set var="postId" value="${param.id}"/>
    <c:set var="post" value="${questionDAO.getQuestionById(postId)}"/>
    <c:set var="answers" value="${answerDAO.getAnswersByQuestionId(postId)}"/>

    <c:if test="${empty post}">
        <div class="card">
            <div class="card-body">
                <h3>帖子不存在</h3>
                <p>该帖子可能已被删除或不存在</p>
                <a href="index.jsp" class="btn btn-primary">返回首页</a>
            </div>
        </div>
    </c:if>

    <c:if test="${not empty post}">
        <!-- 帖子详情 -->
        <div class="card">
            <div class="card-header">
                <h2>${post.title}</h2>
                <div class="post-meta">
                    <span><i class="icon-user"></i>${post.author}</span>
                    <span><i class="icon-calendar"></i>${post.createTime}</span>
                    <span><i class="icon-eye"></i>${post.viewCount}次浏览</span>
                </div>
            </div>
            <div class="card-body">
                <div class="post-content" style="line-height: 1.8; font-size: 1.1rem;">
                        ${fn:escapeXml(post.content)}
                </div>
            </div>
        </div>

        <!-- 评论区域 -->
        <div class="card" style="margin-top: 2rem;">
            <div class="card-header">
                <h3>评论 (${fn:length(answers)})</h3>
            </div>
            <div class="card-body">
                <c:choose>
                    <c:when test="${not empty answers}">
                        <c:forEach items="${answers}" var="answer">
                            <div class="answer-item">
                                <div class="answer-header">
                                    <div class="answer-avatar">${fn:substring(answer.author, 0, 1)}</div>
                                    <div class="answer-author">${answer.author}</div>
                                    <div class="answer-time">${answer.createdAt}</div>
                                </div>
                                <div class="answer-content">
                                        ${fn:escapeXml(answer.content)}
                                </div>
                            </div>
                        </c:forEach>
                    </c:when>
                    <c:otherwise>
                        <div class="empty-state">
                            <p>暂无评论，快来发表第一条评论吧！</p>
                        </div>
                    </c:otherwise>
                </c:choose>
            </div>
        </div>

        <!-- 发表评论 -->
        <c:if test="${not empty sessionScope.user}">
            <div class="card" style="margin-top: 2rem;">
                <div class="card-header">
                    <h3>发表评论</h3>
                </div>
                <div class="card-body">
                    <form method="post" action="answer">
                        <input type="hidden" name="question_id" value="${post.id}">
                        <div class="form-group">
                            <textarea name="content" class="form-control" rows="4" placeholder="写下你的评论..." required></textarea>
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">发表评论</button>
                        </div>
                    </form>
                </div>
            </div>
        </c:if>

        <c:if test="${empty sessionScope.user}">
            <div class="card" style="margin-top: 2rem;">
                <div class="card-body text-center">
                    <p>登录后发表评论</p>
                    <a href="login.jsp" class="btn btn-outline-primary">登录</a>
                </div>
            </div>
        </c:if>
    </c:if>
</main>

<footer class="footer">
    <div class="footer-content">
        <p>&copy; 2025 智界论坛</p>
        <p>科技改变未来</p>
    </div>
</footer>
</body>
</html>